package com.dps.omc.utils.telnet;

import com.dps.omc.agent.queue.consumer.tasker.utils.WindowsTelnetHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.SocketException;

/**
 * Created by DXH on 2016/5/30.
 */
@Service
public class TelnetTest extends TelnetService{
    private static final Logger logger = LoggerFactory.getLogger(TelnetTest.class);
    @Override
    public boolean testConnect(TelnetConnect telnetConnect) throws IOException {

        String host = telnetConnect.getHost();
        String user = telnetConnect.getUser();
        String pass = telnetConnect.getPass();
        WindowsTelnetHelper wth = null;

        try {
            wth = new WindowsTelnetHelper(host,user,pass);
            if (wth.connectToHost()) {
                wth.initIO(null);
                if (wth!=null&&wth.login()) {
                    return true;
                }
            }
        } catch (SocketException e) {

            logger.error(e.toString());
        } catch (IOException e) {

            logger.error(e.toString());
        }
        finally {
            wth.destroy();
        }
        return false;
    }
}
